/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "can.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "simplelib.h"
#include "dji_board_can.h"
//#include "rudder_chassis.h"
#include "omni_chassis.h"
#include "chassis_common_config.h"
#include <string.h>
//#include "vesc_can.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
static uint8_t rx_buf;
static uint8_t rxd_buf;
extern uint8_t state123; //OPENMV状�?�机
int flag_receive = 0;
uint32_t test_ms = 0;
//static uint8_t *rx_ptr = rx_buf;
//uint8_t tx_buf[32] = "error\r\n";
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
extern CAN_Message_u MotorRpmCANMsg[4];
extern uint32_t IR_start_flag;
CAN_TxHeaderTypeDef txHeader;
extern uint32_t IR_cnt_ms;
extern uint32_t c_last_ms;
uint8_t IR_status = AIR;
extern uint8_t openmv[19];
extern char CtrlFlag;
extern int point_groupx[23];
extern int point_groupy[23];
uint8_t state = 0;
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  MX_CAN1_Init();
  MX_USART2_UART_Init();
  MX_USART3_UART_Init();
  MX_UART4_Init();
  /* USER CODE BEGIN 2 */
  //int point_groupx[5]={700,1300,1900,1900,1900};
  //int point_groupy[5]={0,0,0,0,500};
  //int last_point=0;
  txHeader.StdId = 104;
  txHeader.IDE = CAN_ID_STD;
  txHeader.RTR = CAN_RTR_DATA;
  txHeader.DLC = 8;

  SimpleLib_Init(&huart1, &hcan1);
  //for (int i = 0; i < 4; i++)
  // {
  //  VelLoopCfg(CAN1, i + 1, 10, 10);
  // if(i==2)
  //    HAL_Delay(1);
  //}
  for (int i = 0; i < 4; i++)
  {
    VelLoopCfg(CAN1, i + 1, 10, 10);
    MotorOn(CAN1, i + 1); // ʹ��������Ϊ��˲ʱ�Ƕ�ֵΪ0
    if (i == 2)
      HAL_Delay(1);
  }
  for (int i = 0; i < 4; i++)
  {
    VelLoopCfg(CAN1, i + 1, 10, 10);
    MotorOn(CAN1, i + 1); // ʹ��������Ϊ��˲ʱ�Ƕ�ֵΪ0
    if (i == 2)
      HAL_Delay(1);
  }
  for (int i = 0; i < 4; i++)
  {
    VelLoopCfg(CAN1, i + 1, 10, 10);
    MotorOn(CAN1, i + 1); // ʹ��������Ϊ��˲ʱ�Ƕ�ֵΪ0
    if (i == 2)
      HAL_Delay(1);
  }
  for (int i = 0; i < 4; i++)
  {
    VelLoopCfg(CAN1, i + 1, 10, 10);
    MotorOn(CAN1, i + 1); // ʹ��������Ϊ��˲ʱ�Ƕ�ֵΪ0
    if (i == 2)
      HAL_Delay(1);
  }
  for (int i = 0; i < 4; i++)
  {
    VelLoopCfg(CAN1, i + 1, 10, 10);
    MotorOn(CAN1, i + 1); // ʹ��������Ϊ��˲ʱ�Ƕ�ֵΪ0
    if (i == 2)
      HAL_Delay(1);
  }
  HAL_UART_Receive_IT(&huart2, &rx_buf, 1);
  HAL_UART_Receive_IT(&huart4, &rxd_buf, 1);
  OmniChassis_Init();
  //Chassis_Go2Point_Reset();
  Chassis_TrackStatus_Reset();
  OmniChassis.base->Go2PointStatus.target_yaw = 1.57;
  OmniChassis.base->Go2PointStatus.enable = 1;
  OmniChassis.base->Go2PointStatus.target_point.x = 668;
  OmniChassis.base->Go2PointStatus.target_point.y = -4;
  OmniChassis.base->ctrl_mode = CTRL_MODE_GO_TO_POINT;
  //OmniChassis.base->ctrl_mode = CTRL_MODE_TUNING;
  //OmniChassis.base->ctrl_mode = CTRL_MODE_CMD;
  //OmniChassis.base->ctrl_mode = CTRL_MODE_TRACK;
  //OmniChassis.base->ctrl_mode = CTRL_MODE_HANDLE;
  //OmniChassis.base->ctrl_mode = CTRL_MODE_TUNING;
  OmniChassis.base->Go2PointStatus.enable_always_yaw_ctrl = 1;
  OmniChassis.base->Go2PointStatus.disable_yaw_ctrl = 0;
  //OmniChassis.drive_motors[0].can_id=104;
  //PosLoopCfg(CAN1, 1, 10, 10,1000);
  Chassis_RemappingPathSetsSpeed(points_pos1, points_pos1_num, 0.01, 0, 0.1, 1.6);
  //Chassis_RemappingPathSetsSpeed(points_pos2, points_pos2_num, 0.01, 0, 0.1, 1.6);
  BaseChassis.TrackStatus.start = 1;
  /*if (HAL_GPIO_ReadPin(kaiguan_GPIO_Port, kaiguan_Pin) == 1)
  {
    for (int i = 7; i < 23; i++)
    {
      point_groupy[i] = point_groupy[i] + 75;
    }
    for (int i = 12; i < 23; i++)
    {
      point_groupy[i] = point_groupy[i] + 50;
    }
    point_groupx[22] = -4;
    point_groupy[22] = 4520;
  }*/
  //BaseChassis.ctrl_mode = CTRL_MODE_NONE;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    SimpleLib_Run();
    //if(OmniChassis.base->PostureStatus.y>-400&&OmniChassis.base->PostureStatus.y<400)
    //  IR_status=AIR;
    //else if(OmniChassis.base->PostureStatus.y>600&&OmniChassis.base->PostureStatus.y<1400)
    //  IR_status=BIR;
    // else if(OmniChassis.base->PostureStatus.y>1600&&OmniChassis.base->PostureStatus.y<2400)
    //  IR_status=CIR;
    //else if(OmniChassis.base->PostureStatus.y>2600&&OmniChassis.base->PostureStatus.y<3400)
    //  IR_status=DIR;

    //if(OmniChassis.base->Go2PointStatus.enable==0&&last_point!=4)
    //{
    //  OmniChassis.base->Go2PointStatus.enable=1;
    //  OmniChassis.base->Go2PointStatus.start=1;
    //  BaseChassis.Go2PointStatus.start=1;
    //  last_point++;
    //  OmniChassis.base->Go2PointStatus.target_point.x=point_groupx[last_point];
    //  OmniChassis.base->Go2PointStatus.target_point.y=point_groupy[last_point];
    //  OmniChassis.base->ctrl_mode= CTRL_MODE_WAIT;
    //  IR_start_flag =1 ;
    //  IR_cnt_ms=0;
    //}

    //DJI_VelCrl(CAN1 , 1, 200);
    //DJI_PosCtrl(CAN1,1,ABSOLUTE_MODE,10000000);
    //comm_can_set_rpm(100, 1000);
    //RudderChassis_Exe();
    //OmniChassis_Exe();
    //uprintf("%f , %f", Mode1_pos_x, Mode1_pos_y);
    //HAL_Delay(20);

    //float motor_speed[4];
    //motor_speed[0] = 10;
    //motor_speed[1] = 10;
    //motor_speed[2] = 10;
    //motor_speed[3] = 10;
    //int16_t motor_speed_set[4];

    //if (OmniChassis.motor_can_flag)
    //{
    // for (int i = 0; i < 4; i++)
    // {
    //     motor_speed_set[i]=(int16_t)(motor_speed[i] / DRIVE_WHEEL_RADIUS * 30.0f / PI);
    //  }
    //  DJI_velCtrAll(motor_speed_set);
    //   for (int i = 0; i < 4; i++)
    //  {
    //       MotorRpmCANMsg[i].in[0] = 1;
    //       MotorRpmCANMsg[i].in[1] = (int)(motor_speed[i] / DRIVE_WHEEL_RADIUS * 30.0f / PI); //m/s转为rpm
    //        CAN_SendStdMsg(OmniChassis.drive_motors[i].can_id, &MotorRpmCANMsg[i]);
    //txHeader.StdId=OmniChassis.drive_motors[i].can_id;
    //uprintf("%f",OmniChassis.drive_motors[i].can_id );
    //HAL_CAN_AddTxMessage(slib_hcan, &txHeader,  MotorRpmCANMsg[i].ui8, &TxMailbox);
    //       HAL_Delay(1);
    //    }
    //   OmniChassis.motor_can_flag = 0;
    //}

    // if(flag_receive==1)
    //{

    if (CtrlFlag)
    {
      IR_exe();
      OmniChassis_Exe();
      CtrlFlag = 0;
      //uprintf("x:%7.2f y:%7.2f\r\n",BaseChassis.PostureStatus.x,BaseChassis.PostureStatus.y);

      // uprintf("x:%9.2f y:%9.2f now_yaw:%5.2f %5.2f\r\n", BaseChassis.PostureStatus.x,
      //         BaseChassis.PostureStatus.y, BaseChassis.PostureStatus.yaw,BaseChassis.target_yaw);
    }

    if(TimeFlag_50ms)
    {
    }
    //if(HAL_GPIO_ReadPin(gas_pull_GPIO_Port, gas_pull_Pin)==1)
    // uprintf("1");
    //else
    // uprintf("0");
    //}

    //Omni_ChassisMove( OmniChassis.base->target_speed , OmniChassis.base->target_dir , OmniChassis.base->target_omega );
    // if (CtrlFlag)
    //{
    // Omni_ChassisMove( 0 , 0, 0.5 );
    //Omni_ChassisMove( 0.5 , -1.571 , 0 );
    //CtrlFlag = 0;
    //}
    //if(state123==2)
    //HAL_UART_Transmit(&huart2, &rx_buf, 1 , HAL_MAX_DELAY);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
static unsigned int TimeFlag_1ms = 0;
unsigned int TimeFlag_10ms = 0;
// ���¶�ʱ��־λ��ת����10ms
char TimeFlag_5ms = 0;
char TimeFlag_1s = 0;
char TimeFlag_20ms = 0;
char TimeFlag_100ms = 0;
char TimeFlag_50ms = 0;
extern uint32_t IR_cnt_ms;
extern uint32_t test_ms;
extern uint32_t c_last_ms;
char CtrlFlag = 0;
/**
 * @brief ��Ƶ��ʱ��
 */
void inc(void)
{
  TimeFlag_1ms++;
  IR_cnt_ms++;
  test_ms++;
  c_last_ms++;
  if (TimeFlag_1ms % 500 == 0) // 500ms
  {
  }

  if (TimeFlag_1ms % 100 == 0) // 100ms
  {
  }

  if (TimeFlag_1ms % 50 == 0) // 50ms=20hz
  {
  }

  //10ms
  if (TimeFlag_1ms % 10 == 0)
  {
    TimeFlag_20ms = (TimeFlag_10ms % 2 == 0) ? 1 : 0;
    TimeFlag_50ms = (TimeFlag_10ms % 5 == 0) ? 1 : 0;
    TimeFlag_100ms = (TimeFlag_10ms % 10 == 0) ? 1 : 0;
    TimeFlag_1s = (TimeFlag_10ms % 100 == 0) ? 1 : 0;
    TimeFlag_10ms++;
    CtrlFlag = 1;
  }

  //5ms
  if (TimeFlag_1ms % 5 == 0)
  {
    //OmniChassis_Exe();
    TimeFlag_5ms = 1;
  }
  else
  {
    TimeFlag_5ms = 0;
  }
  if (TimeFlag_1ms % 50 == 0) // 50ms=20hz
  {
    //RudderChassis.SteerMotors.can_send_flag = 1;
    //RudderChassis.DriveMotors.can_send_flag = 1;
    OmniChassis.motor_can_flag = 1;
    MotorCANSendFlag = 1;
  }
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if (USART2 == huart->Instance)
  {
    Openmv_Receive_Data(rx_buf);
    HAL_UART_Receive_IT(&huart2, &rx_buf, 1);
  }
  if (UART4 == huart->Instance)
  {
    Openmv_Receive_Data1(rxd_buf);
    HAL_UART_Receive_IT(&huart4, &rxd_buf, 1);
  }
}

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
